package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ioftv.entity.ManagerPerformance;
import com.ioftv.mapper.ManagerPerformanceMapper;
import com.ioftv.service.ManagerPerformanceService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 项目经理绩效Service实现
 */
@Service
public class ManagerPerformanceServiceImpl extends BaseService<ManagerPerformanceMapper, ManagerPerformance> implements ManagerPerformanceService {

    @Override
    public List<ManagerPerformance> getRanking(Integer year, Integer month, Integer limit) {
        LambdaQueryWrapper<ManagerPerformance> wrapper = createUserQueryWrapper();
        wrapper.eq(ManagerPerformance::getStatYear, year);
        wrapper.eq(ManagerPerformance::getStatMonth, month);
        wrapper.orderByDesc(ManagerPerformance::getCompletedCount);
        
        if (limit != null && limit > 0) {
            wrapper.last("LIMIT " + limit);
        }
        
        return this.list(wrapper);
    }
    
    @Override
    public List<ManagerPerformance> getRankingByUserId(Integer year, Integer month, Integer limit, Long userId) {
        LambdaQueryWrapper<ManagerPerformance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManagerPerformance::getUserId, userId);
        wrapper.eq(ManagerPerformance::getStatYear, year);
        wrapper.eq(ManagerPerformance::getStatMonth, month);
        wrapper.orderByDesc(ManagerPerformance::getCompletedCount);
        
        if (limit != null && limit > 0) {
            wrapper.last("LIMIT " + limit);
        }
        
        return this.list(wrapper);
    }

    @Override
    public boolean refreshRanking(Integer year, Integer month) {
        // 查询该月份所有数据并按完成数量排序
        List<ManagerPerformance> list = this.getRanking(year, month, null);
        
        // 更新排名
        AtomicInteger rank = new AtomicInteger(1);
        list.forEach(performance -> {
            performance.setRanking(rank.getAndIncrement());
            this.updateById(performance);
        });
        
        return true;
    }
    
    @Override
    public boolean refreshRankingByUserId(Integer year, Integer month, Long userId) {
        // 查询该用户该月份所有数据并按完成数量排序
        List<ManagerPerformance> list = this.getRankingByUserId(year, month, null, userId);
        
        // 更新排名
        AtomicInteger rank = new AtomicInteger(1);
        list.forEach(performance -> {
            performance.setRanking(rank.getAndIncrement());
            this.updateById(performance);
        });
        
        return true;
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<ManagerPerformance> queryWrapper) {
        queryWrapper.eq(ManagerPerformance::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(ManagerPerformance entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}